How to convert arrays and strings in PHP (must read)

您所在的位置:网站首页 php 转换为字符串 How to convert arrays and strings in PHP (must read)

How to convert arrays and strings in PHP (must read)

2024-06-26 08:01:35| 来源: 网络整理| 查看: 265

In PHP, arrays and strings can be converted. Today we will introduce the method of converting arrays and strings. Friends in need can refer to it.

How to convert arrays and strings in PHP (must read)

1. Convert array to stringimplode

Method introduction:

function implode ($glue = "", array $pieces) {}Copy after login

Mainly has two parameters

One is the connector (glue: the meaning of glue), the default is an empty string

The other is the array

Use $test = array("hello","world","php"); echo implode("-",$test);Copy after login

Result:

hello-world-phpCopy after loginCopy after login

What if it is an array in the form of K-V?

$test = array("h"=>"hello","w"=>"world","p"=>"php"); echo implode("-",$test);Copy after login

The result is still

hello-world-phpCopy after loginCopy after login

, indicating that it still only affects value.

2. Split the string into an array2.1 Split by a certain characterfunction explode ($delimiter, $string, $limit = null) {}Copy after login

explode (explode, maybe explode the string?)

Parameter explanation:

delimiter, delimiter

limit

Document google translation results:

If limit is set to positive, the returned array will Contains the maximum limiting element, with the last element containing the remainder of the string.

Personal understanding:

limit limits the number of split parts. The last part is the remaining string after splitting. Of course, if it is 1, it is the string itself (has passed Experiment)

Code demonstration:

$str="hello-world-php"; $result = explode("-", $str); var_dump($result); $result = explode("-", $str,2); var_dump($result);Copy after login

Output result:

array(3) { [0]=> string(5) "hello" [1]=> string(5) "world" [2]=> string(3) "php" } array(2) { [0]=> string(5) "hello" [1]=> string(9) "world-php" }Copy after login2.2 Reading by distance

Maybe the string does not need to be divided, but each one needs to be Take out the characters, that is, read them out one by one

Original document:

** * Convert a string to an array * @link http://php.net/manual/en/function.str-split.php * @param string $string

* The input string. *

* @param int $split_length [optional]

* Maximum length of the chunk. *

* @return array If the optional split_length parameter is * specified, the returned array will be broken down into chunks with each * being split_length in length, otherwise each chunk * will be one character in length. * *

* false is returned if split_length is less than 1. * If the split_length length exceeds the length of * string, the entire string is returned as the first * (and only) array element. * @since 5.0 */ function str_split ($string, $split_length = 1) {}

Copy after login

Partial translation:

Array If the optional split_length parameter is specified, the returned array will is split into chunks of length split_length, otherwise each chunk would be one character long.

Convert a string to an array, isn’t this just converting a string into an array, there is nothing to say about the usage

Now I want to give it a try, if it is 5 characters in length The string is read in two digits. If there are less than two digits, should the last digit be retained?

Of course, presumably for the sake of data integrity, it should be retained.

$str = "hello"; var_dump(str_split($str,2));Copy after login

The result is just as I expected

array(3) { [0]=> string(2) "he" [1]=> string(2) "ll" [2]=> string(1) "o" }Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to convert arrays and strings in PHP (must read). For more information, please follow other related articles on the PHP Chinese website!



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭